package com.sample.navigation.fragment.dialog;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.sample.navigation.R;
import com.sample.navigation.fragment.Fragment1;
import com.sample.navigation.layout.BubbleLayout;
import com.sample.navigation.util.ScreenMetricUtil;
import com.sample.navigation.util.WindowStyleUtil;

import java.util.Timer;
import java.util.TimerTask;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public class FragmentDialogProgress extends DialogFragment {
    private final int MSG_UPDATE_PROGRESS = 0;
    private Fragment1 parent;
    private View rootView;
    private BubbleLayout progressBubble;
    private TextView progressText;
    private ProgressBar progressBar;
    private int progress = 0;
    private EventHandler handler = new EventHandler();
    private class EventHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_UPDATE_PROGRESS:{
                    progressBar.setProgress(progress);
                    progressText.setText(String.format("%d%%", progress));
                    Rect rtProgress = new Rect();
                    progressBar.getGlobalVisibleRect(rtProgress);
                    Rect rtBubble = new Rect();
                    progressBubble.getLocalVisibleRect(rtBubble);
                    progressBubble.setX(rtProgress.left + rtProgress.width()* progress / 100 - rtBubble.width()/2);
                    progressBubble.setY(rtProgress.top  - rtBubble.height() - 20);
                    break;
                }
            }
        }
    }

    private void sendMsg(int what, Object obj){
        Message msg = new Message();
        msg.what = what;
        msg.obj = obj;
        handler.sendMessage(msg);
    }

    public FragmentDialogProgress(Fragment1 parent){
        this.parent = parent;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.dialog_progress, null);
        initComponents();
        return rootView;
    }

    private void initComponents(){
        rootView.findViewById(R.id.btn_close)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });
        progressBar = (ProgressBar)rootView.findViewById(R.id.progress_view);
        progressBar.setProgress(0);
        progressBubble = rootView.findViewById(R.id.progress_bubble);
        progressText = rootView.findViewById(R.id.progress_text);


        //用来模拟进度条变化
        Timer timer = new Timer();
        TimerTask countTask = new TimerTask() {
            @Override
            public void run() {
                progress++;
                if(progress == 100){
                    dismiss();
                    parent.switchToNext();
                }
                sendMsg(MSG_UPDATE_PROGRESS, null);
            }
        };
        timer.schedule(countTask, 0, 100);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (getDialog() != null) {
            Window window = getDialog().getWindow();
            if (window != null) {
                window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                        | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                WindowStyleUtil.setNavigationColor(window, Color.TRANSPARENT);
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        if (window != null) {
            window.setBackgroundDrawableResource(android.R.color.transparent);
            // 一定要设置Background，如果不设置，window属性设置无效
            if (getActivity() != null) {
                WindowManager windowManager = getActivity().getWindowManager();
                if (windowManager != null) {
                    WindowManager.LayoutParams params = window.getAttributes();
                    params.gravity = Gravity.CENTER;
                    params.width = (int)(ScreenMetricUtil.getWidth() - 60*ScreenMetricUtil.getDensity());
                    params.height = (int)(220*ScreenMetricUtil.getDensity());
                    window.setAttributes(params);
                }
            }
        }
    }
}
